package com.fu.plugin.trees.nodes;

import com.fu.plugin.navigation.MyNavigationItem;
import com.intellij.psi.PsiElement;
import com.intellij.ui.treeStructure.CachingSimpleNode;
import com.intellij.ui.treeStructure.SimpleNode;
import com.intellij.ui.treeStructure.SimpleTree;

import java.awt.event.InputEvent;
import java.util.ArrayList;
import java.util.List;

public class MyTreeNode extends CachingSimpleNode {
    private SimpleNode parent;
    private MyNavigationItem myNavigationItem;
    private List<MyTreeNode> children = new ArrayList<>();
    private String name;

    public MyTreeNode(String name, SimpleNode parent, PsiElement psiElement){
        super(parent);
        this.getTemplatePresentation().setIcon(null);
        this.setIcon(null);
        this.parent = parent;
        if(null != psiElement){
            this.myNavigationItem = new MyNavigationItem(name, psiElement);
        }

        this.name = name;
    }

    @Override
    protected SimpleNode[] buildChildren() {
        return children.parallelStream().toArray(SimpleNode[]::new);
    }

    public List<MyTreeNode> children(){
        return children;
    }

    @Override
    public String getName(){
        return name;
    }

    @Override
    public void handleDoubleClickOrEnter(SimpleTree tree, InputEvent inputEvent) {
        SimpleNode selectedNode = tree.getSelectedNode();
        if(selectedNode instanceof MyTreeNode){
            MyTreeNode myTreeNode = (MyTreeNode) selectedNode;
            if(null != myTreeNode.myNavigationItem){
                myNavigationItem.navigate(true);
            }
        }
    }
}
